package com.lhy.service.impl;

import com.lhy.dao.DoctorDao;
import com.lhy.dao.MemberDao;
import com.lhy.dao.UserDao;
import com.lhy.domain.Doctors;
import com.lhy.domain.User;
import com.lhy.service.UserService;
import com.lhy.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 汪
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    MemberDao memberDao;
    @Autowired
    DoctorDao doctorDao;

    @Override
    public User findUser(String username, String password) {
        return userDao.queryByUserNameAndPassword(username, password);
    }

    @Override
    public int updatePassword(String password1, String password2) {
        return userDao.updatePassword(password1, password2);
    }

    @Override
    public List<User> userShow(Pagination<User> page) {
        return userDao.userShow(page);
    }

    @Override
    public List<User> findSomeUser(Integer id, String realName, String role) {
        return userDao.findSomeUser(id, realName, role);
    }

    @Override
    public int addUser(User u) {
        return userDao.addUser(u);
    }

    @Override
    public int deleteUser(int id) {
        User userById = userDao.findUserById(id);
        if (userById.getRole().equals("会员")) {
            memberDao.deleteMember(Integer.parseInt(userById.getUsername()));
            return userDao.deleteUser(id);
        } else if (userById.getRole().equals("医生")) {
            Doctors doctors = doctorDao.getDoctorByName(userById.getUsername());
            doctorDao.deleteDoctor(doctors.getDocId());
            return userDao.deleteUser(id);
        } else {
            return userDao.deleteUser(id);
        }
    }

    @Override
    public User findUserById(Integer id) {
        return userDao.findUserById(id);
    }

    @Override
    public int updateUser(User u) {
        return userDao.updateUser(u);
    }

    @Override
    public List<User> findSomeUser1(Pagination<User> page) {
        return userDao.findSomeUser1(page);
    }
}
